#!/bin/bash
cd `dirname $0`
source armbian-sysinfo

case $1 in
        get_throttled) echo "throttled=0x0";;
#        measure_temp) echo "temp=$board_temp'C";;
        get_config)
                case $2 in
                        total_mem)
                                NUM=$( free -m | grep Mem: | awk '{print $2}' )
                                if [ -e /var/log/dmesg ]; then
                                        KB=$( sudo grep 'Memory:' /var/log/dmesg | awk '{print $5}' | cut -d'/' -f2 | sed 's/K//g' | head -1 )
                                else
                                        KB=""
                                fi

                                if [[ "$KB" == "" ]]; then
                                        GB=`echo "( $NUM + 256 ) / 256" | bc`
                                        MB=$( echo "${GB} * 256" | bc )
                                else
                                        GB=$( echo "($KB + 2048) / 1024 / 256" | bc )
                                        MB=$( echo "${GB} * 256" | bc )
                                fi
                                echo "total_mem=$MB"
                                ;;
                        *)
                                echo "invalid option";;
                esac
                ;;
        measure_clock)
                case ${2} in
                        arm)
                                # awk is probably overkill....
                                value=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | awk '{print $1 * 1000}'`
                                echo 'frequency(45)='${value}
                                exit
                        ;;
                        core)
                                value=0  # TODO / FIXME
                                echo 'frequency(1)='${value}
                                exit
                        ;;
                # TODO anything else thrown an error/debug
                esac
                exit
                ;;
        measure_temp)
                # awk is probably overkill....
                value=`cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1 / 1000}'`
                echo 'temp='${value}"'C"
                exit
                ;;
        measure_volts)
                case ${2} in
                        core)
                                value=1  # TODO / FIXME
                                echo 'volt='${value}'.0000V'
                                exit
                        ;;
                # TODO anything else thrown an error/debug
                esac
                ;;
        version)
                echo 'Nov  4 2018 16:31:07'
                echo 'Copyright (c) 2012 rock64'
                echo 'version rock64_TODO (clean) (release)'
                exit
                ;;
esac
